﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : Object_Basic
{
    public Vector2 OriginalSpeed;
    public int reward_brick;
    public float Multiply_Speed = 1;
    public float Add_ByTime;
    public float Max_Speed_2 = 64;
    Rigidbody2D body;
    public int Reward_Brick
    {
        get
        {
            return reward_brick;
        }
        set
        {
            if (value >= 1)
            {
                reward_brick=value;
            }
            else
            {
                reward_brick = 10;
            }
        }
    }
    int brick_hit;
    //public GameObject generatorEvent;
    public string EdgeTag="Edge";
    public string BrickTag = "Brick";
    override protected void Start()
    {
        base.Start();
        Reward_Brick = reward_brick;
        brick_hit = 0;
        body = FatherObject.GetComponent<Rigidbody2D>();
        body.velocity = OriginalSpeed;
        //Debug.Log("Speed:" + Speed);
    }
    protected override void Remove(GameObject obj)
    {
        base.Remove(obj);
        Ball_G.Presented--;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == EdgeTag)
        {
            Remove(FatherObject);
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == BrickTag)
        {
            brick_hit++;
            if (brick_hit >= Reward_Brick)
            {
                GameObject.Find("ObjectPool").GetComponent<Ball_G>().Generate();
                brick_hit = 0;
            }
        }
    }
    private void FixedUpdate()
    {
        float x = body.velocity.x;
        float y = body.velocity.y;
        if (x * x + y * y < Max_Speed_2)
        {
            Multiply_Speed += Add_ByTime;
            body.velocity = new Vector2(x * Multiply_Speed, y * Multiply_Speed);
        }
        if (body.velocity == Vector2.zero)
        {
            body.velocity = OriginalSpeed;
        }
    }
    //private void OnBecameInvisible()
    //{

    //    Remove(FatherObject);
    //}
}
